package com.jeeplus.modules.kpi.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.kpi.dao.AchievementInfoDao;
import com.jeeplus.modules.kpi.entity.AchievementInfo;
import com.jeeplus.modules.kpi.entity.AchievementInfoScore;
import com.jeeplus.modules.sys.dao.ResultDao;
import com.jeeplus.modules.sys.echartDto.ResultVo;
import com.jeeplus.modules.sys.entity.Result;

/**
 * 绩效考核流程Service
 * @author wangkai
 * @version 2017-12-25
 */
@Service
@Transactional(readOnly = true)
public class AchievementInfoService extends CrudService<AchievementInfoDao, AchievementInfo> {
	
	@Autowired
	private ResultDao<Result> resultDao;
	@Autowired
	private AchievementInfoScoreService achievementInfoScoreService;
	
	
	public AchievementInfo findByPublisher(ResultVo resultVo){
		// 科研成果发布量
			AchievementInfo achievementInfo = new AchievementInfo();
			//查询分数信息
			List<AchievementInfoScore> findList = achievementInfoScoreService.findList(new AchievementInfoScore());
			//对应成果信息
			List<Result> resultRepub = resultDao.findByPublisher(resultVo);
			for (Result result2 : resultRepub) {
			//课题项目得分计算
				//国家科研项目得分
				if(StringUtils.equals(result2.getProjectType(), "A")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.outsideNationalLevel.name(), findList);
					Double funds = calculate(score,result2.getFunds(),achievementInfo.getOutsideNationalLevel());
					achievementInfo.setOutsideNationalLevel(funds);
				}
				//自治区项目得分
				if(StringUtils.equals(result2.getProjectType(), "C")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.outsideAutonomousRegionLevel.name(), findList);
					Double funds = calculate(score,result2.getFunds(),achievementInfo.getOutsideAutonomousRegionLevel());
					achievementInfo.setOutsideAutonomousRegionLevel(funds);
				}
				//院外其他项目得分
				if(StringUtils.equals(result2.getProjectType(), "D")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.outsideOtherLevel.name(), findList);
					Double funds = calculate(score,result2.getFunds(),achievementInfo.getOutsideOtherLevel());
					achievementInfo.setOutsideOtherLevel(funds);
				}
				//院内项目得分
				if(StringUtils.equals(result2.getProjectType(), "C")) {
					//院内院重大课题
					if(StringUtils.equals(result2.getTopicType(), "A")) {
						BigDecimal score = getScore(AchievementInfoScore._Type.insideGreatTask.name(), findList);
						Double funds = calculate(score,result2.getFunds(),achievementInfo.getInsideGreatTask());
						achievementInfo.setInsideGreatTask(funds);
					}
					//院内院重点课题
					if(StringUtils.equals(result2.getTopicType(), "B")) {
						BigDecimal score = getScore(AchievementInfoScore._Type.insideImportance_task.name(), findList);
						Double funds = calculate(score,result2.getFunds(),achievementInfo.getInsideImportance_task());
						achievementInfo.setInsideImportance_task(funds);
					}
					//院内院精品课题
					if(StringUtils.equals(result2.getTopicType(), "C")) {
						BigDecimal score = getScore(AchievementInfoScore._Type.insideBoutique_task.name(), findList);
						Double funds = calculate(score,result2.getFunds(),achievementInfo.getInsideBoutique_task());
						achievementInfo.setInsideBoutique_task(funds);
					}
					//院内院一般课题
					if(StringUtils.equals(result2.getTopicType(), "D")) {
						BigDecimal score = getScore(AchievementInfoScore._Type.insideGeneral_task.name(), findList);
						Double funds = calculate(score,result2.getFunds(),achievementInfo.getInsideGeneral_task());
						achievementInfo.setInsideGeneral_task(funds);
					}
					//院内院青年课题
					if(StringUtils.equals(result2.getTopicType(), "E")) {
						BigDecimal score = getScore(AchievementInfoScore._Type.insideYouthTask.name(), findList);
						Double funds = calculate(score,result2.getFunds(),achievementInfo.getInsideYouthTask());
						achievementInfo.setInsideYouthTask(funds);
					}
				}
			//成果发表得分计算	
				//每个成果对应一部
				String volume = "1";
				//著作(专著)的得分
				if(StringUtils.equals(result2.getAchievementForm(), "C1")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.workMonograph.name(), findList);
					Double funds = calculate(score,volume,achievementInfo.getWorkMonograph());
					achievementInfo.setInsideYouthTask(funds);
				}
				//("著作(编著)")
				if(StringUtils.equals(result2.getAchievementForm(), "C2")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.workWrite.name(), findList);
					Double funds = calculate(score,volume,achievementInfo.getWorkWrite());
					achievementInfo.setWorkWrite(funds);
				}
				//著作(译著)
				if(StringUtils.equals(result2.getAchievementForm(), "C8")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.workTranslation.name(), findList);
					Double funds = calculate(score,volume,achievementInfo.getWorkTranslation());
					achievementInfo.setWorkTranslation(funds);
				}
				
				//("著作(其他著作)"
				if(StringUtils.equals(result2.getAchievementForm(), "C10")) {
					BigDecimal score = getScore(AchievementInfoScore._Type.workOther.name(), findList);
					Double funds = calculate(score,volume,achievementInfo.getWorkOther());
					achievementInfo.setWorkOther(funds);
				}
				//还有论文等等， 论文又包括 （SCI、SSCI、AHCI），（《人民日报》《光明日报》《经济日报》《求是》），（全国核心），（院定核心	），（一般报刊）等
				//目前没分清 还不能计算
			}
			return achievementInfo;
	}
	private Double calculate(BigDecimal score, String funds, Double srcScore) {
		if(srcScore == null) {
			srcScore = 0.0;
		}
		if(score != null && funds  != null  && funds.matches("-?[0-9]+.?[0-9]*")) {
			  BigDecimal calcScore = score.multiply(new BigDecimal(funds)).add(new BigDecimal(srcScore));
			  return  calcScore.doubleValue() ;
		}
		return srcScore;
	}
	private BigDecimal getScore(String catelog,List<AchievementInfoScore> findList) {
		for (AchievementInfoScore achievementInfoScore : findList) {
			if(StringUtils.equals(achievementInfoScore.getCateLog(), catelog)) {
				return achievementInfoScore.getScore();
			}
		}
		return null;
	}
	
	public AchievementInfo get(String id) {
		return super.get(id);
	}
	
	public List<AchievementInfo> findList(AchievementInfo achievementInfo) {
		return super.findList(achievementInfo);
	}
	
	public Page<AchievementInfo> findPage(Page<AchievementInfo> page, AchievementInfo achievementInfo) {
		return super.findPage(page, achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(AchievementInfo achievementInfo) {
		super.save(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(AchievementInfo achievementInfo) {
		super.delete(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void insertProcess(AchievementInfo achievementInfo) {
		achievementInfo.preInsert();
		dao.insertProcess(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void updateDepartmentExamine(AchievementInfo achievementInfo) {
		dao.updateDepartmentExamine(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void updateScientificExamine(AchievementInfo achievementInfo) {
		dao.updateScientificExamine(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void updatePoliticalExamine(AchievementInfo achievementInfo) {
		dao.updatePoliticalExamine(achievementInfo);
	}
	
	@Transactional(readOnly = false)
	public void updateProcessNode(AchievementInfo achievementInfo) {
		dao.updateProcessNode(achievementInfo);
	}
	
}